/*=============================================================
(c) all rights reserved

GraydonDataServices examples

================================================================*/
using System;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
using System.Xml;

/**
 * @author denisreilly
 *
 * Runnable .net 2.0 client for Graydon Company Data Webservice
 * 
 * Operation : GetCompanyReport
 * 
 * Argument0 = Partner Userid
 * Argument1 = Partner Password
 * Argument2 = Order Reference
 */
class PlaceOrder{   
   public static void Main(string[] args)
   {
      
       try
	   {
	      string partnerUserid             = null;
	      string partnerPassword	       = null;
	      string orderReference            = null;
	      
	      // retrieve arguments
	      if (args.Length == 0)
			throw new Exception("Invalid arguments: Please specify match arguments");
			
	      // populate match parameters with arguments
	      partnerUserid               = args[0].Trim();
	      partnerPassword             = args[1].Trim();
	      orderReference              = args[2].Trim();
	            
	      GraydonCompanyData graydonCompanyData = new GraydonCompanyData();
	      
	      // create the authentication parameters
	      Authentication_ParametersType authentication_Parameters                                    = new Authentication_ParametersType();
	      authentication_Parameters.PartnerUserId                                                    = partnerUserid;
	      authentication_Parameters.PartnerPassword                                                  = partnerPassword;
	      
	      // create the parameters
	      GetCompanyReport_ParametersType getCompanyReport_Parameters                                = new GetCompanyReport_ParametersType();
	      getCompanyReport_Parameters.Authentication_Parameters                                      = authentication_Parameters;
	      getCompanyReport_Parameters.OrderReference                                                 = Convert.ToInt32(orderReference);
	      getCompanyReport_Parameters.MimeType                                                       = MimeTypesType.xml;

	      // Begin the Async call to "getCompanyReport"
	      IAsyncResult ar = graydonCompanyData.BegingetCompanyReport(getCompanyReport_Parameters, null, graydonCompanyData);
	      
	      // wait for response
	      ar.AsyncWaitHandle.WaitOne();
	      
	      GetCompanyReport_ResultType getCompanyReport_Result = null;
	      
	      try
          {
             // get the results of the match
             getCompanyReport_Result = graydonCompanyData.EndgetCompanyReport(ar);
          }
          catch (SoapException soapE)
          {
            Console.WriteLine(" ");
            // SOAP 1.1 Fault info
            Console.WriteLine("SOAP 1.1 fault...");
            Console.WriteLine("Code:/t/t" + soapE.Code.ToString());
            Console.WriteLine("Actor:/t/t" + soapE.Actor);
            Console.WriteLine("Detail:/t/t" + soapE.Detail.InnerXml);
            throw new Exception("Fault occurred whilst accessing the webservice.");
          }
          
         // get the service log details
         serviceLog(getCompanyReport_Result.Service_Log);
         
		 // check if report have been populated
		 if (getCompanyReport_Result.ReportXML != null)
		 {
			Console.WriteLine("Report Response:");
			Console.WriteLine("  Mime Type\t: " + getCompanyReport_Result.ReportMimeType);	
			
			// check the report mimetype
			if (getCompanyReport_Result.ReportMimeType == MimeTypesType.xml)
			{
				Console.WriteLine("Report :");
				XmlTextWriter writer    = new XmlTextWriter(Console.Out);
                writer.Formatting       = Formatting.Indented;
                getCompanyReport_Result.ReportXML.WriteContentTo(writer);
			}	
		 }
	  }  
      catch (Exception e)
      {
	     Console.WriteLine("Exception : " + e);
      }

   }
   
   	/**
	* Service log output
	*
	*/
	private static void serviceLog(Service_LogType service_Log)
	{
		Console.WriteLine("Service_Log :");																	
		Console.WriteLine("  PartnerUserId\t\t: " + service_Log.PartnerUserId);												    // output partner userid
		Console.WriteLine("  SessionId\t\t: " + service_Log.SessionID);														    // output sessionId
		Console.WriteLine("  Transaction Identifier: " + service_Log.TransactionIdentifier);									// output transaction identifier
		Console.WriteLine("  Request Date\t\t: " + "{0:d}", service_Log.RequestTimestamp.Date);				                    // output request date
		Console.WriteLine("  Request Time\t\t: " + "{0:t}", service_Log.RequestTimestamp.Time);							        // output request time
		Console.WriteLine("  Response Date\t\t: " + "{0:d}", service_Log.ResponseTimestamp.Date);				                // output response date
		Console.WriteLine("  Response Time\t\t: " + "{0:t}", service_Log.ResponseTimestamp.Time);								// output response time
		Console.WriteLine(" ");
	}
   
}